import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_layout/main/base_stateful_widget.dart';
import 'package:get/get.dart';

class DialogPage extends BaseStatefulWidget {
  const DialogPage({Key? key}) : super(key: key);

  @override
  _DialogPageState createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.runtimeType.toString()),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ListView(
          children: [
            ElevatedButton(
              child: const Text("AboutDialog"),
              onPressed: () {
                showDialogDemo(context);
              },
            ),
            AboutListTile(
                icon: const FlutterLogo(),
                child: const Text("AboutList"),
                applicationName: "标题",
                applicationVersion: "1.0.0",
                applicationLegalese: "我是一个很有内涵的人",
                aboutBoxChildren: [
                  const Text("Box"),
                  const Text("Box"),
                  RichText(
                      text: TextSpan(children: [
                    TextSpan(
                        style: Theme.of(context).textTheme.bodyText1,
                        text: 'Flutter is Google’s UI toolkit for building beautiful, '
                            'natively compiled applications for mobile, web, and desktop '
                            'from a single codebase. Learn more about Flutter at '),
                    TextSpan(
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {}
                          ..onTapDown = (t) {
                            widget.showAppToast("点击了" + t.globalPosition.dx.toString());
                          },
                        onEnter: (event) {
                          widget.showAppToast("点击了");
                        },
                        onExit: (event) {
                          widget.showAppToast("点击了onExit");
                        },
                        style: Theme.of(context).textTheme.bodyText1!.copyWith(color: Colors.amber),
                        text: 'https://flutter.dev'),
                    TextSpan(style: Theme.of(context).textTheme.bodyText1, text: '.'),
                  ]))
                ],
                applicationIcon: const Icon(Icons.close)),
            ElevatedButton(
              child: const Text("AlertDialog"),
              onPressed: () {
                showAlertDialogDemo(page: widget, context: context);
              },
            ),
            ElevatedButton(
              child: const Text("IOSDialog"),
              onPressed: () {
                showIOSDialogDemo(page: widget, context: context);
              },
            ),
            ElevatedButton(
              child: const Text("SimpleDialog"),
              onPressed: () {
                // showDialog(context: context, builder: )
                simpleDialog(context);
              },
            ),
            ElevatedButton(
                child: const Text("Getx Dialog"),
                onPressed: () {
                  Get.defaultDialog(
                      title: "标题",
                      content: const Text("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容"),
                      radius: 10,
                      barrierDismissible: false,
                      textCancel: "取消",
                      textConfirm: "确定",
                      onConfirm: () {
                        Get.back();
                      },
                      contentPadding: const EdgeInsets.all(30));
                }),
          ],
        ),
      ),
    );
  }
}

void showAlertDialogDemo({DialogPage? page, context}) async {
  var result = await showDialog(
      context: context,
      builder: (builder) {
        return AlertDialog(
          title: const Text(
            "温馨提示",
            textAlign: TextAlign.center,
          ),
          backgroundColor: Colors.blue[100],
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          content: const Text("确认删除吗？"),
          actionsPadding: const EdgeInsets.all(0),
          buttonPadding: const EdgeInsets.all(0),
          titlePadding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
          actions: [
            Flex(
              direction: Axis.horizontal,
              children: [
                Expanded(
                  child: InkWell(
                    child: const Padding(
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      child: Text(
                        "取消",
                        textAlign: TextAlign.center,
                      ),
                    ),
                    onTap: () {
                      Get.back(result: "取消啊");
                      // page?.showAppToast("取消");
                    },
                  ),
                  flex: 1,
                ),
                Expanded(
                  child: InkWell(
                    child: const Padding(
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      child: Text(
                        "确定",
                        textAlign: TextAlign.center,
                      ),
                    ),
                    onTap: () {
                      Get.back(result: "确定啊");
                      // page?.showAppToast("确定");
                    },
                  ),
                  flex: 1,
                ),
              ],
            )
          ],
        );
      });
  // AlertDialog(title: Text("标题"),);
  page!.showAppToast(result);
}

void showIOSDialogDemo({DialogPage? page, context}) async {
  // showCupertinoDialog(
  //     context: context,
  //     builder: (context) {
  //       return CupertinoAlertDialog(
  //         title: Text('提示'),
  //         content: Text('确认删除吗？'),
  //         actions: <Widget>[
  //           CupertinoDialogAction(
  //             child: Text('取消'),
  //             onPressed: () {},
  //           ),
  //           CupertinoDialogAction(
  //             child: Text('确认'),
  //             onPressed: () {},
  //           ),
  //         ],
  //       );
  //     });
}

void simpleDialog(context) {
  // SimpleBuilder(builder: {
  // });
  // Fluttertoast.showToast(msg: "123");
  // showDialog(context: context,builder: SimpleDialog(title: Text(""),children: [],));
  Future.delayed(const Duration(seconds: 3), () {
    return Future.error('');
  });

  // ignore: unused_local_variable
  Dialog dialog = const Dialog(
    backgroundColor: Colors.white,
    elevation: 10,
    child: Text("自定义Dialog，只有一个Text"),
  );

  SimpleDialog dialog2 = SimpleDialog(
    title: const Text('提示'),
    children: <Widget>[
      Container(
        height: 80,
        alignment: Alignment.center,
        child: const Text('确认删除吗？'),
      ),
      const Divider(
        height: 1,
        color: Colors.lightBlue,
      ),
      MaterialButton(
        child: const Text('取消'),
        onPressed: () {
          Navigator.of(context).pop('cancel');
        },
      ),
      const Divider(
        height: 1,
      ),
      MaterialButton(
        child: const Text('确认'),
        onPressed: () {
          Navigator.of(context).pop('ok');
        },
      ),
    ],
  );

  // ignore: unused_local_variable
  var dialog3 = SimpleDialog(
    title: const Text('选择'),
    children: <Widget>[
      SimpleDialogOption(
        child: const Text('选项 1'),
        onPressed: () {
          Navigator.of(context).pop("1111");
        },
      ),
      SimpleDialogOption(
        child: const Text('选项 2'),
        onPressed: () {
          Navigator.of(context).pop("2222");
        },
      ),
    ],
  );

  var result = showDialog<String>(
    context: context,
    builder: (BuildContext context) {
      return dialog2;
    },
  ).then((val) {
    if (kDebugMode) {
      print("val-----$val");
    }
  });

  if (kDebugMode) {
    print(result);
  }

  //  Navigator.of(context, rootNavigator: false).push<void>(DialogRoute<void>(
  //   context: context,
  //   builder: dialog2,
  //   // barrierColor: barrierColor,
  //   // barrierDismissible: barrierDismissible,
  //   // barrierLabel: barrierLabel,
  //   // useSafeArea: useSafeArea,
  //   // settings: routeSettings,
  //   // themes: themes,
  // ));
  // Dialog(backgroundColor: Colors.white,elevation: 10,child: Text("啥阿道夫"),);
}

void showDialogDemo(context) {
  showAboutDialog(
      context: context,
      applicationName: "普通ialog",
      applicationVersion: "1.0.0",
      applicationLegalese: "更新内容如下：",
      applicationIcon: const Icon(Icons.verified_user),
      children: [
        const Padding(
          padding: EdgeInsets.only(top: 10),
          child: Text("1、优化了"),
        ),
        const Text("2、多功能"),
        const Text("3、语音聊天"),
      ],
      useRootNavigator: false,
      routeSettings: const RouteSettings(name: "11", arguments: "2233"));
}

void showDialogListDemo(context) {
  showDialog(
      context: context,
      builder: (BuildContext builder) {
        return const AboutListTile(
            icon: FlutterLogo(),
            child: Text("很好"),
            applicationName: "标题",
            applicationLegalese: "我是一个很有内涵的人",
            aboutBoxChildren: [
              Text("Box"),
              Text("Box"),
              Text("Box"),
              Text("Box"),
            ],
            applicationIcon: Icon(Icons.close));
      });
}
